package cst.neu.neufooddemo.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cst.neu.neufooddemo.beans.LoginBean;
import cst.neu.neufooddemo.R;
import cst.neu.neufooddemo.listener.RetrofitListener;
import cst.neu.neufooddemo.model.UserModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener ,RetrofitListener<LoginBean>{
    private EditText username,userpass;
    private TextView register;
    private Button button_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }



    private void initViews() {
        username= (EditText) findViewById(R.id.username);
        userpass= (EditText) findViewById(R.id.password);
        button_login= (Button) findViewById(R.id.button_login);
        register= (TextView) findViewById(R.id.textView_register);
    }
    private void initEvents() {
//        button_login.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        button_login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button_login:
                //处理登录的点击事件
                if ("".equals(username.getText().toString())||"".equals(userpass.getText().toString())){
                    Toast.makeText(this, "用户名和密码为空", Toast.LENGTH_SHORT).show();
                }
                else {
                    login();
                }
                break;
            case R.id.textView_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
        }
    }

    private void login() {
//        Retrofit retrofit=new Retrofit.Builder()//建立Retrofit对象
//                .baseUrl(Constans.BASE_URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        UserService userService=retrofit.create(UserService.class);
//        Call<LoginBean>call=userService.userLogin(username.getText().toString(),userpass.getText().toString());
//        Callback<LoginBean>callback=new Callback<LoginBean>() {
//            @Override
//            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
//                String userid=response.body().getUserid();//获取用户id
//                if("0".equals(userid)){
//                    //登录失败
//                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
//                }
//                else {
//                    //登录成功
//                    saveUserInfo(userid);//传参
//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
//                    startActivity(intent);
//                    finish();
//                }
//            }
//
//            @OvernFairide
//            public void olure(Call<LoginBean> call, Throwable t) {
//                t.printStackTrace();
//                Toast.makeText(LoginActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
//            }
//        };
//        call.enqueue(callback);
        UserModel model=new UserModel();
        model.userLogin(username.getText().toString(),userpass.getText().toString(),LoginActivity.this);

    }
    public void saveUserInfo(String user_id){
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("username",username.getText().toString());
        int userid=Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();


    }




    @Override
    public void onSuccess(LoginBean userId, int flag) {
        if (userId.getUserid().equals("0")) {
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        } else {
            saveUserInfo(userId.getUserid());
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String user=data.getStringExtra("user");
        username.setText(user);
    }
}
